package com.kalvan.web.servlet.response;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 统一返回数据扩展
 *
 * @author chenliang
 */
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class ResponseHandler implements ResponseBodyAdvice {
//    @Reference
//    ISign iSign;

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        log.debug("response");
//        if (o instanceof Result) {
//            log.info("返回明文结果:{}", LogFormatUtil.formatBean(o));
//            ApiContext apiContext = ApiContextHolder.getContext();
//            if (null == apiContext) {
//                log.debug("上下文对象为空，没有进入拦截器则不需要加解密，明文返回");
//                return o;
//            }
//            if (TestController.test_merchant.contains(apiContext.getMerchantNo())) {
//                log.debug("测试商户号直接不加密返回明文");
//                return o;
//            }
//            Result result = (Result) o;
//            Long merchantNo = apiContext.getMerchantNo();
//            String json = JSON.toJSONString(result);
//            Map<String, String> signMap = iSign.rsaSignPlaintext(merchantNo, json);
//            log.info("正常响应报文:{}", JSON.toJSONString(signMap));
//            return signMap;
//        }
        return o;

    }
}
